// cQuickPlaceDB.cpp : implementation file
//

#include "stdafx.h"
#include "qpmig.h"
#include "cQuickPlaceDB.h"
#include "cNotesFolderInfo.h"
#include "cUser.h"
#include "noteshelperfunctions.h"
extern "C" __declspec( dllimport )int GetMime(DBHANDLE hDB, NOTEID NoteID, char *szBuf, int buflen);


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CQPMigApp theApp;

/////////////////////////////////////////////////////////////////////////////
// cQuickPlaceDB

IMPLEMENT_DYNCREATE(cQuickPlaceDB, CObject)

cQuickPlaceDB::cQuickPlaceDB(LPCSTR lpszNotesServerCN,LPCSTR lpszNotesDatabaseCN,LPCSTR lpszName)
{
	strcpy(szNotesServerCN,lpszNotesServerCN);
	strcpy(szNotesDatabaseCN,lpszNotesDatabaseCN);
	strName=lpszName;
	bstrDescription=NULL;
	CString cInfo;
	cInfo.Format("This is the QuickPlace %s", lpszName);
	bstrDescription=cInfo.AllocSysString();

}
cQuickPlaceDB::cQuickPlaceDB()
{
	bstrDescription=NULL;

}
cQuickPlaceDB::~cQuickPlaceDB()
{
	if(db.IsOpen())
		db.Close();
	for(int i=0;i<=cobjNotesFolders.GetUpperBound();i++)
		{
			cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)cobjNotesFolders.GetAt(i);
			if(pFolderInfo)
				delete pFolderInfo;
		}
	for(i=0;i<=cobjArUsers.GetUpperBound();i++)
		{
			CUser *pUser=(CUser *)cobjArUsers.GetAt(i);
			if(pUser)
				delete pUser;
		}
	if(bstrDescription)
		::SysFreeString(bstrDescription);

}
bool cQuickPlaceDB::Process()
	{
		theApp.stControl.pDebugLog->LogOneLine("Entering cQuickPlaceDB::Process()");
		theApp.stControl.pDebugLog->Flush();
	

		LNSTATUS Status;
		if(LNNOERROR==theApp.stControl.session.GetDatabase(szNotesDatabaseCN, &db,szNotesServerCN))
			{
				// Open the database
				if(LNNOERROR!=(Status=db.Open()))
					{
						char errorBuf[LNERROR_MESSAGE_LENGTH];
						LNGetErrorMessage(Status, errorBuf);
						AfxMessageBox(errorBuf,MB_OK|MB_ICONERROR); 
						theApp.stControl.pDebugLog->LogOneLine(errorBuf);
						return false;
					}
				theApp.stControl.pDebugLog->LogOneLine(szNotesDatabaseCN);
				theApp.stControl.pDebugLog->Flush();


			}
		else
			return false;
		bool bRetValue=true;
		if(db.IsOpen()) //Verify that the db is open
			{
				//Add the default folders Calendar,Tasks
				cNotesFolderInfo *pFolderInfo=new cNotesFolderInfo;
				if(pFolderInfo)
					{
						pFolderInfo->m_strFolderDisplayName="Calendar";
						pFolderInfo->m_strNotesName="h_Calendar";
						pFolderInfo->m_wType=CALENDARFOLDER;
						if(!theApp.stControl.m_bAdvanced)
							pFolderInfo->b_Transfer=TRUE;
						cobjNotesFolders.Add(pFolderInfo);
						
					}
				pFolderInfo=new cNotesFolderInfo;
				if(pFolderInfo)
					{
						pFolderInfo->m_strFolderDisplayName="Quickplace Tasks";
						pFolderInfo->m_strNotesName="h_TaskList";
						pFolderInfo->m_wType=TASKFOLDER;
						if(!theApp.stControl.m_bAdvanced)
							pFolderInfo->b_Transfer=TRUE;
						cobjNotesFolders.Add(pFolderInfo);
					}

				LNViewFolder     folder;
				LNVFNavigator nav;          // Working navigator
				LNVFEntry        entry;
				theApp.stControl.pDebugLog->LogOneLine("Enumerating through the Folder list");
				if(LNNOERROR==db.GetViewFolder("h_Folders", &folder))  // Get list of folders
					{
						if(LNNOERROR==folder.Open())
							{
								folder.GetEntries(&nav);
								folder.GotoFirst(&entry);
								if (folder.GetEntryCount() >0)
									{
										do
											{
												LNDocument	Doc;
												LNString Value;
												LNText oneItem;
												
												if(LNNOERROR==entry.GetDocument(&Doc))
													{
														if(LNNOERROR==Doc.Open())
															{

																if(LNNOERROR==Doc.GetItem("h_Name",&oneItem))
																	{
																		cNotesFolderInfo *pFolderInfo=new cNotesFolderInfo;
																		if(pFolderInfo)
																			{
																				pFolderInfo->m_wType=DOCFOLDER;
																				Value=oneItem[0];
																				pFolderInfo->m_strFolderDisplayName=Value;
																				pFolderInfo->m_strFolderDisplayName.TrimLeft();
																				pFolderInfo->m_strFolderDisplayName.TrimRight();
																				theApp.stControl.pDebugLog->LogOneLine(Value);
																				theApp.stControl.pDebugLog->Flush();
																				if(LNNOERROR==Doc.GetItem("h_FolderStyle",&oneItem))
																					{
																						Value=oneItem[0];
																						if("5"==Value)
																							pFolderInfo->m_wType=DISCFOLDER;
																						if("h_Calendar"==Value)
																							pFolderInfo->m_wType=CALENDARFOLDER;
																						if("h_Task"==Value)
																							pFolderInfo->m_wType=TASKFOLDER;

																					}


																				if(LNNOERROR==Doc.GetItem("h_SystemName",&oneItem))
																					{
																						Value=oneItem[0];
																						pFolderInfo->m_strNotesName=Value;
																						if(!theApp.stControl.m_bAdvanced)
																							pFolderInfo->b_Transfer=TRUE;
																						cobjNotesFolders.Add(pFolderInfo);

																					}
																				else
																					delete pFolderInfo; //No system name given, hence the item cannot be opened with that name
																			}
																	}
																Doc.Close();
															}
													}
												
											} while (nav.GotoNextMain(&entry) != LNWARN_NOT_FOUND);
									}
								folder.Close();   // Close the folder

							}
					}
				theApp.stControl.pDebugLog->LogOneLine("Enumerating through the Subroom list");
				if(LNNOERROR==db.GetViewFolder("h_AreaView", &folder))  // Get list of folders
					{
						if(LNNOERROR==folder.Open())
							{
								folder.GetEntries(&nav);
								folder.GotoFirst(&entry);
								if (folder.GetEntryCount() >0)
									{
					

										do
											{
												LNDocument	Doc;
												LNString Value;
												LNText oneItem;
												
												if(LNNOERROR==entry.GetDocument(&Doc))
													{
														if(LNNOERROR==Doc.Open())
															{

																if(LNNOERROR==Doc.GetItem("h_Name",&oneItem))
																	{
																		cNotesFolderInfo *pFolderInfo=new cNotesFolderInfo;
																		if(pFolderInfo)
																			{
																				Value=oneItem[0];
																				pFolderInfo->m_strFolderDisplayName=Value;
																				pFolderInfo->m_strFolderDisplayName.TrimLeft();
																				pFolderInfo->m_strFolderDisplayName.TrimRight();
																				CString cLastPart;
																				int nPos=pFolderInfo->m_strFolderDisplayName.ReverseFind('\\');
																				if(nPos>0)
																					cLastPart=pFolderInfo->m_strFolderDisplayName.Right(nPos);
																				else
																					cLastPart=pFolderInfo->m_strFolderDisplayName;

																				theApp.stControl.pDebugLog->LogOneLine(Value);
																				theApp.stControl.pDebugLog->Flush();

																				if(LNNOERROR==Doc.GetItem("h_LocDbName",&oneItem))
																					{
																						Value=oneItem[0];
																						theApp.stControl.pDebugLog->LogOneLine("Subroom DB");
																						theApp.stControl.pDebugLog->LogOneLine(Value);
																						theApp.stControl.pDebugLog->Flush();


																						pFolderInfo->m_strNotesName=Value;
																						pFolderInfo->m_wType=SUBROOM;
																						if(!theApp.stControl.m_bAdvanced)
																							pFolderInfo->b_Transfer=TRUE;

																						cobjNotesFolders.Add(pFolderInfo);
																						if(nPos>0)
																							ProcessSubRoomList(this,pFolderInfo->m_strNotesName,pFolderInfo->m_strFolderDisplayName.Left(nPos),cLastPart);
																						else
																							ProcessSubRoomList(this,pFolderInfo->m_strNotesName,pFolderInfo->m_strFolderDisplayName,cLastPart);
																					}
																				else
																					delete pFolderInfo; //No system name given, hence the item cannot be opened with that name
																		}
																	}
																Doc.Close();
															}
													}
												
											} while (nav.GotoNextMain(&entry) != LNWARN_NOT_FOUND);


									}
								folder.Close();   // Close the folder

							}
					}
				if(LNNOERROR==db.GetViewFolder("h_ViewUIPage", &folder))  
					{
						if(LNNOERROR==folder.Open())
							{
								if(LNNOERROR==folder.Find( "h_Welcome", &entry ))
									{
										LNDocument	Doc;
										LNString Value;
										LNText oneItem;
										
										if(LNNOERROR==entry.GetDocument(&Doc))
											{
												if(LNNOERROR==Doc.Open())
													{
														try
															{
																LNSTATUS error=Doc.GetItem("h_TextAbstract",&oneItem);
																if(LNNOERROR==error)
																	{
																		Value=oneItem[0];
																		CString cInfo=Value;
																		cInfo.TrimLeft();
																		cInfo.TrimRight();
																		bstrDescription=cInfo.AllocSysString();
																	}
																else
																	{
																		char errorBuf[LNERROR_MESSAGE_LENGTH];
																		LNGetErrorMessage(error, errorBuf);
																		theApp.stControl.pLog->LogOneLine("Could not find the description of the QuickPlace. Error:");
																		theApp.stControl.pLog->LogOneLine(errorBuf);
																	}

																/*Create announcement listing */
																IXMLDOMDocument2Ptr pAnnouncementsListDoc=NULL;
																HRESULT hr=pAnnouncementsListDoc.CreateInstance(__uuidof(DOMDocument40));
																if(FAILED(hr))
																	{
																		AfxMessageBox("Could not instantiate the DOMDocument component\r\nPlease make sure that the Microsoft XML Parser is installed",MB_OK|MB_ICONERROR);
																		theApp.stControl.pLog->LogOneLine("Could not instantiate the DOMDocument component\r\nPlease make sure that the Microsoft XML Parser is installed");
																		return FALSE;
																	}

																MSXML2::IXMLDOMProcessingInstructionPtr pPI = pAnnouncementsListDoc->createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");

																_variant_t vNullVal;
																vNullVal.vt = VT_NULL;
																pAnnouncementsListDoc->insertBefore(pPI, vNullVal);
																pPI.Release();

																MSXML2::IXMLDOMElementPtr pRootElem=pAnnouncementsListDoc->createElement(L"list");
																if(pRootElem)
																	{
																		pAnnouncementsListDoc->documentElement=pRootElem;
																		MSXML2::IXMLDOMElementPtr pProfileElem=pAnnouncementsListDoc->createElement(L"profile");
																		if(pProfileElem)
																			{
																				hr=pProfileElem->setAttribute(L"Name", L"QPAnnouncements");
																				pRootElem->appendChild(pProfileElem);
																				
																				MSXML2::IXMLDOMElementPtr pAnnouncementNode=pAnnouncementsListDoc->createElement(L"item");
																				if(pAnnouncementNode)
																					{
																						theApp.stControl.NrOfListItems++;
																						pProfileElem->appendChild(pAnnouncementNode);
																						MSXML2::IXMLDOMElementPtr pFieldsElem=pAnnouncementsListDoc->createElement(L"field");
																						MSXML2::IXMLDOMElementPtr pNode=NULL;
																						if(pFieldsElem)
																							{
																								pNode=pAnnouncementsListDoc->createElement(L"name");
																								pNode->put_text(L"Title");
																								pFieldsElem->appendChild(pNode);
																								pNode.Release();
																								
																								pNode=pAnnouncementsListDoc->createElement(L"type");
																								pNode->put_text(L"Text");
																								pFieldsElem->appendChild(pNode);
																								pNode.Release();
																								
																								pNode=pAnnouncementsListDoc->createElement(L"value");
																								pNode->put_text(L"Welcome");
																								pFieldsElem->appendChild(pNode);
																								pNode.Release();
																					
																								pAnnouncementNode->appendChild(pFieldsElem);
																								pFieldsElem.Release();
																							
																							}
																						char *pszBuf=(char *)malloc(64*1024);
																						
																						if(pszBuf)
																							{
																								memset(pszBuf,'\0',64*1024);
																								int nLen=GetMime(db.GetDatabaseHandle(TRUE), Doc.GetNoteID(), pszBuf, 64*1024);
																								if(nLen>0)
																									{
																										pFieldsElem=pAnnouncementsListDoc->createElement(L"field");
																										if(pFieldsElem)
																											{
																												pNode=pAnnouncementsListDoc->createElement(L"name");
																												pNode->put_text(L"Body");
																												pFieldsElem->appendChild(pNode);
																												pNode.Release();
																												
																												pNode=pAnnouncementsListDoc->createElement(L"type");
																												pNode->put_text(L"Note");
																												pFieldsElem->appendChild(pNode);
																												pNode.Release();
																												
																												pNode=pAnnouncementsListDoc->createElement(L"value");
																												CString cInfo=pszBuf;
																												free(pszBuf);
																												BSTR bstr=cInfo.AllocSysString();
																												pNode->put_text(bstr);
																												pFieldsElem->appendChild(pNode);
																												::SysFreeString(bstr);
																												pNode.Release();
																												
																												pAnnouncementNode->appendChild(pFieldsElem);
																												pFieldsElem.Release();
																											
																											}

																									}
																								else
																									{
																										pFieldsElem=pAnnouncementsListDoc->createElement(L"field");
																										if(pFieldsElem)
																											{
																												pNode=pAnnouncementsListDoc->createElement(L"name");
																												pNode->put_text(L"Body");
																												pFieldsElem->appendChild(pNode);
																												pNode.Release();
																												
																												pNode=pAnnouncementsListDoc->createElement(L"type");
																												pNode->put_text(L"Text");
																												pFieldsElem->appendChild(pNode);
																												pNode.Release();
																												
																												pNode=pAnnouncementsListDoc->createElement(L"value");
																												pNode->put_text(bstrDescription);
																												pFieldsElem->appendChild(pNode);
																												pNode.Release();

																												pAnnouncementNode->appendChild(pFieldsElem);
																												pFieldsElem.Release();
																											
																											}

																									}
																							}

																						CString cFpath;
																						cFpath.Format("%s\\welcomeitems.xml",theApp.stControl.m_strCurDir);
																						
																						BSTR bstr=cFpath.AllocSysString();
																						pAnnouncementsListDoc->save(bstr);
																						::SysFreeString(bstr);
																						
																						pAnnouncementNode.Release();


																					}
																				pProfileElem.Release();
																			}
												

																	}
																pAnnouncementsListDoc.Release();
																
															}
														catch(LNSTATUS error)
															{
																char errorBuf[LNERROR_MESSAGE_LENGTH];
																LNGetErrorMessage(error, errorBuf);
																theApp.stControl.pDebugLog->LogOneLine(errorBuf);
															}
														catch(CException *cEx)
															{
																char szError[255];
																if(cEx->GetErrorMessage(szError,255))
																	theApp.stControl.pDebugLog->LogOneLine(szError);
																theApp.stControl.pDebugLog->Flush();
																cEx->Delete();
															}
														Doc.Close();
													}
											}
									}

								folder.Close();   // Close the folder

							}
					}
				}
			else
				bRetValue=false;
		theApp.stControl.pDebugLog->LogOneLine("Exiting cQuickPlaceDB::Process()");
		theApp.stControl.pDebugLog->Flush();

		return bRetValue;


																																		

	}
